package com.youxi.qingyuan;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/9.
 */

public class Wuziqi extends View {
    private int mPanelWidth;
    private float mLineHight;
    private int MAX_LINE = 10;

    private Paint mPaint = new Paint();
    private Bitmap mWhitePiece;
    private Bitmap mBlackPiece;
    private float ratioPieceOfLineHight = 3 * 1.0f / 4;

    private boolean mIsWith = true;
    private List<Point> mWitearry = new ArrayList<Point>();
    private List<Point> mBlackarry = new ArrayList<Point>();

    private boolean mIsGemOver;
    private boolean mIsWhiteWinner;

    public Wuziqi(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint.setColor(0x44ff0000);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setStyle(Paint.Style.STROKE);

        mWhitePiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_w2);
        mBlackPiece = BitmapFactory.decodeResource(getResources(), R.drawable.stone_b1);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (mIsGemOver){
            return false;
        }
        int action=event.getAction();
        if (action==MotionEvent.ACTION_UP){
            int x= (int) event.getX();
            int y= (int) event.getY();
            Point p=getVaLidPoint(x,y);
            if (mWitearry.contains(p)||mBlackarry.contains(p)){
                return false;
            }
            if (mIsWith){
                mWitearry.add(p);
            }else {
                mBlackarry.add(p);
            }
            invalidate();
            mIsWith = !mIsWith;
        }
        return true;
    }

    private Point getVaLidPoint(int x, int y) {
        return new Point((int) (x / mLineHight), (int) (y / mLineHight));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSize= MeasureSpec.getSize(widthMeasureSpec);
        int widthMode=MeasureSpec.getMode(widthMeasureSpec);

        int heightSize=MeasureSpec.getSize(heightMeasureSpec);
        int heightMode=MeasureSpec.getMode(heightMeasureSpec);

        int width=Math.min(widthSize,heightSize);

        if (widthMode==MeasureSpec.UNSPECIFIED){
            width=heightSize;
        }else if(heightMode==MeasureSpec.UNSPECIFIED){
            width=widthSize;
        }
        setMeasuredDimension(width, width);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mPanelWidth=w;
        mLineHight=mPanelWidth*1.0f/MAX_LINE;
        int PiceWhite = (int) (mLineHight * ratioPieceOfLineHight);
        mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece, PiceWhite, PiceWhite, false);
        mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece, PiceWhite, PiceWhite, false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawBoard(canvas);
        draePicec(canvas);
        checkGameOver();
    }

    private void checkGameOver() {
        boolean whiteWin=chechFiveInLine(mWitearry);
        boolean blickWin=chechFiveInLine(mBlackarry);
        if (whiteWin||blickWin){
            mIsGemOver=true;
            mIsWhiteWinner=whiteWin;
            String text=mIsWhiteWinner?"白棋胜利":"黑棋胜利";
            Toast.makeText(getContext(),text,Toast.LENGTH_SHORT).show();
        }
    }

    private boolean chechFiveInLine(List<Point> mWitharry2) {
        for(Point p:mWitharry2){
            int x=p.x;
            int y=p.y;
            boolean win=checkHorizontal(x,y,mWitharry2);
            if (win)return true;
            win=checkVertIcal(x,y,mWitharry2);
            if (win) return true;
            win=checkLeftDiagonal(x,y,mWitharry2);
            if (win)return true;
            win=checkRightDiagonl(x,y,mWitharry2);
            if (win)return true;
        }
        return false;
    }

    private boolean checkVertIcal(int x, int y, List<Point> mWitharry2) {
            int count=1;
            for (int i=1;i<5;i++) {
                if (mWitharry2.contains(new Point(x,y-i))) {
                    count++;
                } else {
                    break;
                }
            }
            if (count==5)return true;
        for (int i=1;i<5;i++){
            if (mWitharry2.contains(new Point(x,y+i))){
                count++;
            }else {
                break;
            }
            if (count==5)return true;
        }
        return false;
    }

    private boolean checkRightDiagonl(int x, int y, List<Point> mWitharry2) {
        int count = 1;
        for (int i = 1; i < 5; i++) {
            if (mWitharry2.contains(new Point(x-i,y-i))) {
                count++;
            }else {
                break;
            }
        }
        if (count==5) return true;
        for (int i = 1; i < 5; i++) {
            if (mWitharry2.contains(new Point(x+i,y+i))) {
                count++;
            }else {
                break;
            }
            if (count==5) return true;
        }
        return false;
    }

    private boolean checkLeftDiagonal(int x, int y, List<Point> mWitharry2) {
        int count = 1;
        for (int i = 1; i < 5; i++) {
            if (mWitharry2.contains(new Point(x-i,y+i))) {
                count++;
            }else {
                break;
            }
        }
        if (count==5) return true;
        for (int i = 1; i < 5; i++) {
            if (mWitharry2.contains(new Point(x+i,y-i))) {
                count++;
            }else {
                break;
            }
            if (count==5) return true;
        }
        return false;
    }

    private boolean checkHorizontal(int x, int y, List<Point> mWitharry2) {
        int count=1;
        for (int i=1;i<5;i++){
            if (mWitharry2.contains(new Point(x-1,y))){
                count++;
            }else{
                break;
            }
        }
        if (count==5)return true;
        for (int i=1;i<5;i++){
            if (mWitharry2.contains(new Point(x+1,y))){
                count++;
            }else {
                break;
            }
            if (count==5)return true;
        }
        return false;
    }

    private void draePicec(Canvas canvas) {
        for (int i=0,n=mWitearry.size();i<n;i++){
            Point whitePoint=mWitearry.get(i);
            canvas.drawBitmap(mWhitePiece,(whitePoint.x+(1-ratioPieceOfLineHight)/2)*mLineHight,
                    (whitePoint.y+(1-ratioPieceOfLineHight)/2)*mLineHight,null);
        }
        for (int i=0,n=mBlackarry.size();i<n;i++){
            Point blackPoint=mBlackarry.get(i);
            canvas.drawBitmap(mBlackPiece,(blackPoint.x+(1-ratioPieceOfLineHight)/2)*mLineHight,
                    (blackPoint.y+(1-ratioPieceOfLineHight)/2)*mLineHight,null);
        }
    }

    private void drawBoard(Canvas canvas) {
        int w=mPanelWidth;
        float lineHeight=mLineHight;
        for (int i=0;i<MAX_LINE;i++){
            int startX= (int) (lineHeight/2);
            int endX= (int) (w-lineHeight/2);

            int y= (int) ((0.5+i)*lineHeight);
            canvas.drawLine(startX,y,endX,y,mPaint);
            canvas.drawLine(y,startX,y,endX,mPaint);
        }
    }
}
